package ynu.edu.Controller;

import com.auth0.jwt.JWT;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ynu.edu.Entity.Result;
import ynu.edu.Entity.User;
import ynu.edu.Service.Impl.UserService;
import ynu.edu.Util.JWTUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public String Login(@RequestParam("username") String username, @RequestParam("password") String password) throws Exception{

        User user = userService.findByUserName(username);
        if (user != null && Objects.equals(user.getPassword(), password)) {
            String token = JWTUtil.sign(username, user.getUserId());
            Map<String, String> jsonResponse = new HashMap<>();
            jsonResponse.put("token", token);
            return Result.success(jsonResponse);
        }
        return Result.error("用户名不存在或密码错误");
    }
    @GetMapping("/verify")
    public String verify() {
        return Result.success();
    }
}
